//
//  ViewController.m
//  Animation
//
//  Created by 社区580 on 2018/1/12.
//  Copyright © 2018年 LY. All rights reserved.
//

#import "ViewController.h"
#import "PopView.h"
#import <QuartzCore/QuartzCore.h>

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (assign, nonatomic) BOOL isClicked;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.iconImageView.layer.anchorPoint = CGPointMake(0, 1);
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (_isClicked) {
        _isClicked = NO;
        self.iconImageView.layer.transform = CATransform3DIdentity;
        self.iconImageView.center = CGPointMake(-KScreenWidth/2, KScreenHeight/2);
    }else {
        _isClicked = YES;
        CATransform3D rotate = CATransform3DMakeRotation(M_PI/2.5, 0, -1, 0);
        self.iconImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 500);
        
        [UIView animateWithDuration:0.6 animations:^{
            self.iconImageView.center = CGPointMake(KScreenWidth/2, KScreenHeight/2);
//            self.iconImageView.frame
        } completion:^(BOOL finished) {
        }];
        
        [UIView animateWithDuration:0.5 delay:0.25 options:0 animations:^{
            self.iconImageView.layer.transform = CATransform3DIdentity;
        } completion:^(BOOL finished) {
        }];
        
    }
}


CATransform3D CATransform3DMakePerspective(CGPoint center, float disZ)
{
    CATransform3D transToCenter = CATransform3DMakeTranslation(-center.x, -center.y, 0);
    CATransform3D transBack = CATransform3DMakeTranslation(center.x, center.y, 0);
    CATransform3D scale = CATransform3DIdentity;
    scale.m34 = -1.0f/disZ;
    return CATransform3DConcat(CATransform3DConcat(transToCenter, scale), transBack);
}


CATransform3D CATransform3DPerspect(CATransform3D t, CGPoint center, float disZ)
{
    return CATransform3DConcat(t, CATransform3DMakePerspective(center, disZ));
}



- (void)test03 {
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(KScreenWidth/2-75, KScreenHeight/2-100, 150, 200);
    layer.position = CGPointMake(KScreenWidth/2, KScreenHeight/2);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
    
    [UIView animateWithDuration:1.0 animations:^{
        
    }];
}

- (void)test02 {
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(KScreenWidth/2-75, KScreenHeight/2-100, 150, 200);
    layer.position = CGPointMake(KScreenWidth/2, KScreenHeight/2);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer];
    
    [NSThread sleepForTimeInterval:1];
    
    [UIView animateWithDuration:10.0 animations:^{
        CGAffineTransform affineTransform_1 = CGAffineTransformMakeTranslation(0, 0);
//        CGAffineTransform affineTransform_2 = CGAffineTransformMakeScale(0.5, 0.5);
//        CGAffineTransform affineTransform_all = CGAffineTransformConcat(affineTransform_1, affineTransform_2);
        CATransform3D transform3D_1 = CATransform3DMakeAffineTransform(affineTransform_1);
        CATransform3D transform3D_2 = CATransform3DRotate(transform3D_1, M_PI/8, 0, 0.5, 0.5);
        layer.transform = transform3D_2;
    }];
    
}

- (void)test01 {
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(-350, KScreenHeight - 50, 150, 200);
    layer.position = CGPointMake(KScreenWidth/2, KScreenHeight/2);
    layer.backgroundColor = [UIColor redColor].CGColor;
    UIView *window = [UIApplication sharedApplication].keyWindow;
    [window.layer addSublayer:layer];
    
    CATransform3D transA = CATransform3DIdentity;
    transA.m34 = -1.0 / 500;
    //    transA = CATransform3DRotate(transA, M_PI/3, 1, 0, 0);
    transA = CATransform3DTranslate(transA, 0, 0, 100);
    layer.transform = transA;
    
    
    
    
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(-350, KScreenHeight/2)];
    [path addLineToPoint:CGPointMake(KScreenWidth/2, KScreenHeight/2)];
    pathAnimation.path = path.CGPath;
    
    CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    transformAnimation.toValue = [NSValue valueWithCATransform3D:transA];
    
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[pathAnimation,transformAnimation];
    group.duration = 0.5;
    [layer addAnimation:group forKey:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
